package com.wanmait.recycle.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Config;
import com.wanmait.recycle.pojo.Dispose;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Log;
import com.wanmait.recycle.pojo.RegCount;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.service.ConfigService;
import com.wanmait.recycle.service.DataService;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.LogService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.service.UserinfoService;

@Controller
@RequestMapping("/manager")
public class SystemController {

	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private ConfigService configService;
	@Resource
	private LogService logService;
	@Resource
	private UserinfoService userinfoService;
	@Resource
	private DataService dataService;
	
	
	@RequestMapping("charts/charts3D")
	public ModelAndView charts3D(){
		ModelAndView mv= new ModelAndView();
		List<Secondtype> secondtypes = secondtypeService.findAll();
		mv.addObject("secondtypes",secondtypes);
		return mv;
	}
	
	@RequestMapping("charts/chartsMonth")
	public ModelAndView chartsMonth(){
		ModelAndView mv= new ModelAndView();
		List<Dispose> disposes = dataService.findTwoList();
		mv.addObject("disposes", disposes);
		String str1 = "";
		for (int i = 1; i <= 9; i++) {
			boolean	f=true;
        	for(Dispose dispose: disposes){
        		if(("2019-0"+i).equals(dispose.getInputTimeMonth()))
        		{
        			f=false;
        			str1=str1+dispose.getChuLiCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str1=str1+"0,";
        		}
		}
		for (int i = 10; i <= 12; i++) {
			boolean	f=true;
			for(Dispose dispose: disposes){
        		if(("2019-"+i).equals(dispose.getInputTimeMonth()))
        		{
        			f=false;
        			str1=str1+dispose.getChuLiCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str1=str1+"0,";
        		}
		}
		int len = str1.length()-1;
		mv.addObject("str1",str1.substring(0,len));
		
		String str2 = "";
		for (int i = 1; i <= 9; i++) {
			boolean	f=true;
        	for(Dispose dispose: disposes){
        		if(("2019-0"+i).equals(dispose.getInputTimeMonth()))
        		{
        			f=false;
        			str2=str2+dispose.getChanShengCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str2=str2+"0,";
        		}
		}
		for (int i = 10; i <= 12; i++) {
			boolean	f=true;
			for(Dispose dispose: disposes){
        		if(("2019-"+i).equals(dispose.getInputTimeMonth()))
        		{
        			f=false;
        			str2=str2+dispose.getChanShengCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str2=str2+"0,";
        		}
		}
		int len2 = str2.length()-1;
		mv.addObject("str2",str2.substring(0,len2));
		
		Date year = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Integer yyyy = Integer.parseInt(sdf.format(year));
		mv.addObject("yyyy",yyyy);
		return mv;
	}
	
	@RequestMapping("charts/chartsPie")
	public void chartsPie(){
		
	}
	
	@RequestMapping("charts/chartsTime")
	public ModelAndView chartsTime(){
		ModelAndView mv= new ModelAndView();
		List<RegCount> counts = userinfoService.selectUserCount();
		mv.addObject("counts", counts);
		String str = "";
		
		 
		for (int i = 1; i <= 9; i++) {
			boolean	f=true;
        	for(RegCount regCount: counts){
        		if(("2019-0"+i).equals(regCount.getInputTimeMonth()))
        		{
        			f=false;
        			str=str+regCount.getRegCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str=str+"0,";
        		}
		
		}
		for (int i = 10; i <= 12; i++) {
			boolean	f=true;
        	for(RegCount regCount: counts){
        		if(("2019-"+i).equals(regCount.getInputTimeMonth()))
        		{
        			f=false;
        			str=str+regCount.getRegCount()+",";
        			break;
        		}
        	}
        		if(f==true){
        			str=str+"0,";
        		}
		
		}
		int len = str.length()-1;
		mv.addObject("str",str.substring(0,len));
		Date year = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Integer yyyy = Integer.parseInt(sdf.format(year));
		mv.addObject("yyyy",yyyy);
		return mv;
	}
	
	
	@RequestMapping("charts/charts3DPie")
	public ModelAndView charts3DPie(){
		ModelAndView mv= new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		
		mv.addObject("firsttypes",firsttypes);
		return mv;
	}
	
	@RequestMapping("system/systemData")
	public void systemData() {
		
	}
	
	@RequestMapping("system/systemMenuAdd")
	public void systemMenuAdd() {
		
	}
	
	@RequestMapping(value="system/systemMenuAdd",method=RequestMethod.POST)
	public String systemMenuAdd2(Config config){
		boolean flag = configService.isConfigByExample(config);

		if(flag) return "redirect:/manager/system/systemMenuAdd?msg=exist";
		configService.insertMenu(config);
		return "redirect:/manager/system/systemMenuChanged";
			
	}
	
	@RequestMapping("system/systemLog")
	public ModelAndView systemLog() {
		ModelAndView mv = new ModelAndView();
		List<Log> logs = logService.findAll();
		mv.addObject("logs", logs);
		return mv;
	}
	
	@RequestMapping("system/systemLogDelete")
	@ResponseBody
	public void systemLogDelete(Integer id) {
		logService.delete(id);
	}
	
	@RequestMapping("system/deleteLogs")
	@ResponseBody
	public void deleteLogs(String ids) {
		logService.deleteLogs(ids);
		
	}
	
	@RequestMapping("system/systemMenuChange" )
	public String systemMenu(Integer[] id,String[] value) {
		configService.update(id,value);
		return "redirect:/manager/system/systemMenuChanged";
	}
	
	@RequestMapping("system/systemMenu")
	public ModelAndView systemMenuList() {
		ModelAndView mv = new ModelAndView();
		List<Config> configs = configService.findAll();
		mv.addObject("configs", configs);
		return mv;
	}
	@RequestMapping("system/systemMenuChanged")
	public ModelAndView systemMenuList2() {
		ModelAndView mv = new ModelAndView();
		List<Config> configs = configService.findAll();
		mv.addObject("configs", configs);
		return mv;
	}
	
	// 模糊搜索+时间段搜索
	@RequestMapping("system/searchLog")
	public ModelAndView searchLog(String searchStr, String beginTime, String endTime)
	{
		ModelAndView mv = new ModelAndView();
		if (searchStr == "") {
			searchStr = null;
		}
		List<Log> logs = logService.findAllBySearch(searchStr, beginTime, endTime);
		Map<String, String> SearchMap = new HashMap<>();
		SearchMap.put("searchStr", searchStr);
		SearchMap.put("beginTime", beginTime);
		SearchMap.put("endTime", endTime);

		mv.addObject("logs", logs);
		mv.addObject("searchStr", searchStr);
		mv.setViewName("/manager/system/systemLog");
		return mv;
	}
}
